package cn.itcast.bos.web.action.common;

import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.util.HashMap;
import java.util.Map;

import org.springframework.data.domain.Page;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;

public abstract class BaseAction<T> extends ActionSupport implements ModelDriven<T>{

	protected T model;
	@Override
	public T getModel() {
		
		return model;
	}
	//构造器创建实例对象
	public BaseAction(){
		Type genericSuperclass = this.getClass().getGenericSuperclass();
		ParameterizedType parameteriedTyped=(ParameterizedType)genericSuperclass;
		Class<T> modelClass=(Class<T>)parameteriedTyped.getActualTypeArguments()[0];
		try {
			model=modelClass.newInstance();
		} catch (InstantiationException | IllegalAccessException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			System.out.println("实例化构造失败");
		}
	}
	
	protected int page;
	protected int rows;
	public void setPage(int page) {
		this.page = page;//总页数
	}
	public void setRows(int rows) {
		this.rows = rows;//给页所有数据的集合
	}
	protected void pushToValueStack(Page<T> page){
		Map<String,Object> map=new HashMap<String,Object>();
		map.put("total", page.getTotalElements());
		map.put("rows",page.getContent());
		ActionContext.getContext().getValueStack().push(map);
	}
	
	
}
